//
//  YJEditGroupNickNameView.m
//  YJB
//
//  Created by YJB on 2016/12/26.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "YJEditGroupNickNameView.h"
#import "masonry.h"

@interface YJEditGroupNickNameView ()
@property(nonatomic,weak)UIButton *containBtn;
@property(nonatomic,weak)UITextField *nickInput;
@end

@implementation YJEditGroupNickNameView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        
        UIButton *containBtn = [[UIButton alloc]init];
        containBtn.backgroundColor = WHITECOLOR;
        containBtn.layer.cornerRadius = 4;
        containBtn.clipsToBounds = YES;
        _containBtn = containBtn;
        [self addSubview:containBtn];
        [containBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(25 * GRIDW);
            make.height.mas_equalTo(220);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.centerY.mas_equalTo(self.mas_centerY).offset(-140);
        }];
        UILabel *topLab = [[UILabel alloc]init];
        topLab.text = @"我在本群中的昵称";
        topLab.textColor = kUIColorFromRGB(0x333333);
        topLab.font = FONT(16);
        [containBtn addSubview:topLab];
        [topLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(15);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(23);
        }];
        
        UITextField *nickInput = [[UITextField alloc]init];
        nickInput.textColor = kUIColorFromRGB(0x333333);
        nickInput.font = FONT(16);
        _nickInput = nickInput;
        nickInput.placeholder = @"请输入群昵称";
        [containBtn addSubview:nickInput];
        [nickInput mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(30);
            make.top.mas_equalTo(topLab.mas_bottom).offset(35);
        }];
        
        UIView *bottomLine = [[UIView alloc]init];
        bottomLine.backgroundColor = kUIColorFromRGB(0xff783d);
        [containBtn addSubview:bottomLine];
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(nickInput.mas_left);
            make.right.mas_equalTo(nickInput.mas_right);
            make.top.mas_equalTo(nickInput.mas_bottom);
            make.height.mas_equalTo(0.5);
        }];
        
        UILabel *bottomLab = [[UILabel alloc]init];
        bottomLab.text = @"设置您在群里的昵称,这个昵称只会在这个群里显示";
        bottomLab.textColor = kUIColorFromRGB(0x666666);
        bottomLab.font = FONT(14);
        bottomLab.numberOfLines = 0;
        [containBtn addSubview:bottomLab];
        [bottomLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(nickInput.mas_left);
            make.right.mas_equalTo(nickInput.mas_right);
            make.top.mas_equalTo(nickInput.mas_bottom).offset(15);
        }];
        
        UIButton *checkBtn = [[UIButton alloc]init];
        checkBtn.titleLabel.font = FONT(16);
        [checkBtn setTitle:@"确定" forState:UIControlStateNormal];
        [checkBtn setTitleColor:kUIColorFromRGB(0xff783d) forState:UIControlStateNormal];
        [checkBtn addTarget:self action:@selector(checkBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [containBtn addSubview:checkBtn];
        [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(70);
        }];
        
        UIButton *cancelBtn = [[UIButton alloc]init];
        cancelBtn.titleLabel.font = FONT(16);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:kUIColorFromRGB(0xff783d) forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [containBtn addSubview:cancelBtn];
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(checkBtn.mas_left);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(60);
        }];
    }
    return self;
}

- (void)show
{
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    self.frame = win.frame;
    [self.nickInput becomeFirstResponder];
    [win addSubview:self];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}

- (void)cancelBtnClick
{
    [self dismiss];
}

- (void)checkBtnClick
{
    if (self.nickInput.text.length == 0)
    {
        [MBProgressHUD showError:@"请输入昵称"];
        return;
    }
    if (self.checkBlock)
        self.checkBlock(self.nickInput.text);
}

- (void)setOriNickName:(NSString *)oriNickName
{
    self.nickInput.text = oriNickName;
}

- (void)dismiss
{
    [self.containBtn removeFromSuperview];
    [self removeFromSuperview];
}

@end
